package com.ccs.demo.controller;

import com.ccs.demo.model.BookInfo;
import com.ccs.demo.model.PageRequest;
import com.ccs.demo.model.PageResult;
import com.ccs.demo.service.BookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;


@Slf4j
@RestController
@RequestMapping("/book")
public class BookController {

    @Autowired
    private BookService bookService;

    //添加图书功能
    @RequestMapping("/add")
    public String addBook(BookInfo bookInfo){
        log.info("添加图书, bookInfo: {}", bookInfo);
        //参数校验
        if (bookInfo==null
                || !StringUtils.hasLength(bookInfo.getBookName())
                || !StringUtils.hasLength(bookInfo.getAuthor())
                || bookInfo.getCount()==null
                || bookInfo.getPrice() ==null
                || !StringUtils.hasLength(bookInfo.getPublish())
                || bookInfo.getStatus()==null ){
            return "参数校验失败";
        }
        try {
            Integer result = bookService.addBook(bookInfo);
            if (result==1){
                return "";
            }
        }catch (Exception e){
            log.error("添加图书发生异常, e: {}", e);
            return "内部错误, 请联系管理员";
        }
        return "内部错误, 请联系管理员";
    }


    @RequestMapping("/getListByPage")
    public PageResult<BookInfo> getListByPage(PageRequest pageRequest){
        log.info("请求图书列表，pageRequest:{}",pageRequest);
        return bookService.getListByPage(pageRequest);
    }


    //查询功能
    @RequestMapping("/queryBookById")
    public List<BookInfo> queryBookById(Integer bookId){
        //日志
        log.info("查询图书信息，bookId:{}",bookId);

        //判断id
        if(bookId==null || bookId<1){
            return null;
        }
        //正常则调用服务层
        return bookService.queryBookById(bookId);
    }

    //更新功能
    @RequestMapping("/updateBook")
    public String updateBook(BookInfo bookInfo){
        //日志
        log.info("更新图书信息，bookInfo:{}",bookInfo);
        //异常
        try {
            Integer result= bookService.updateBook(bookInfo);
            if(result<1){
                return "图书更新失败，请联系管理员";
            }
            return "";
        }catch(Exception e){
            log.error("图书更新发生异常，e:",e);
            return"图书更新失败";
        }
    }

    //删除功能
    public String DeleteBook(Integer bookId){
        //日志
        log.info("删除图书信息,bookId:{}",bookId);
        //异常
        try{
            Integer result=bookService.DeleteBook(bookId);
            if (result < 1) {
               return"图书删除失败，请联系管理员";
            }
            return "";
        }catch(Exception e){
            log.error("图书伤处发生异常，e",e);
            return"图书删除失败，请联系管理员";
        }
    }

    //批量删除功能
    public String batchDeleteBook(@RequestParam List<Integer> ids){
        log.info("批量删除图书,ids:{}",ids);

        Integer result=bookService.batchDeleteBook(ids);
        return result>0 ? "" : "图书删除失败，请联系管理员";

    }





//    @RequestMapping("/add")
//    public String addBook(BookInfo bookInfo){
//        //打印添加图书的日志
//        log.info("添加图书,bookInfo:{}",bookInfo);
//
//        //参数校验
//        if(bookInfo==null
//           || !StringUtils.hasLength(bookInfo.getBookName())
//           || !StringUtils.hasLength(bookInfo.getAuthor())
//           || bookInfo.getCount()==null
//           || bookInfo.getPrice()==null
//           || !StringUtils.hasLength(bookInfo.getPublish())
//           || bookInfo.getStatus()==null){
//            return "参数校验失败";
//        }
//        try{
//            Integer result=bookService.addBook(bookInfo);
//            if(result==1){
//                return " ";
//            }
//        }catch(Exception e){
//            log.error("添加图书发生异常，e:{}",e);
//            return "内部错误，请联系管理员";
//        }
//
//
//    return "内部错误，请联系管理员";
//    }










//    @RequestMapping("/getList")
//    public List<BookInfo> getList(){
////        BookService bookService=new BookService();
//        List<BookInfo> bookInfos= bookService.getList();
//        return bookInfos;
//    }




}
